// =====================================================================================
//  Copyright (C) 2024 by Jiaxing Shao. All rights reserved

//  文 件 名:  GlfwWindow.h
//  作    者:  Jiaxing Shao, 980853650@qq.com
//  创建时间：  2024-08-01 21:58
//  描    述:  
// =====================================================================================
#pragma once

#include "glm/ext/matrix_clip_space.hpp"
#include "glm/ext/matrix_transform.hpp"
#include <functional>

#include <glm/glm.hpp>
#include <glad/glad.h>
#include <GLFW/glfw3.h>

class GlfwWindowManager {
public:
    GlfwWindowManager(int width = 800, int height = 600);
    ~GlfwWindowManager();

private:
    static void framebufferSizeCallback(GLFWwindow *window, int width, int height);
    static void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
    static void mouseCallback(GLFWwindow *window, double xpos, double ypos);
    static void scrollCallback(GLFWwindow *window, double xoffset, double yoffset);
    void        processKeyboardInput();

public:
    bool initWindow(const char *title);
    void loopRender(std::function<void()> lambda);

    glm::vec3 const &getCameraPos() const
    {
        return m_cameraPos;
    }

    glm::vec3 const &getCameraFront() const
    {
        return m_cameraFront;
    }

    glm::mat4 getProj()
    {
        return glm::perspective(glm::radians(m_fov), (float)m_width / (float)m_height, 0.1f, 100.0f);
    }

    glm::mat4 getView()
    {
        return glm::lookAt(m_cameraPos, m_cameraPos + m_cameraFront, glm::vec3(0.0f, 1.0f, 0.0f));
    }

private:
    GLFWwindow *m_window;
    int         m_width;
    int         m_height;

    float m_deltaTime;
    float m_lastFrame;

private:
    static glm::vec3 m_cameraPos;
    static glm::vec3 m_cameraFront;
    static glm::vec3 m_cameraUp;

    static float m_fov;   // 视野
    static float m_yaw;   // 偏航角
    static float m_pitch; // 俯仰角

    static bool  m_firstMouse;
    static float m_lastXPos;
    static float m_lastYPos;
};
